<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="医生姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入医生姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="医生年龄" prop="age">
        <el-input
          v-model="queryParams.age"
          placeholder="请输入医生年龄"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="职位" prop="positionId">
<!--        <el-input-->
<!--          v-model="queryParams.positionId"-->
<!--          placeholder="请输入职位id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
        <el-select v-model="queryParams.positionId" placeholder="请选择医生职位">
          <el-option
            v-for="item in workers"
            :label="item.pName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="科室" prop="departmentId">
<!--        <el-input-->
<!--          v-model="queryParams.departmentId"-->
<!--          placeholder="请输入科室id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--        <el-select v-model="queryParams.departmentId" placeholder="请选择科室">-->
<!--          <el-option-->
<!--            v-for="item in depts"-->
<!--            :label="item.name"-->
<!--            :value="item.id">-->
<!--          </el-option>-->
<!--        </el-select>-->
        <el-cascader
          v-model="treeIds"
          :options="trees"
          :props="areaTree"
          @change="searchClick"></el-cascader>
      </el-form-item>
      <el-form-item label="擅长" prop="proficientIn">
<!--        <el-input-->
<!--          v-model="queryParams.proficientIn"-->
<!--          placeholder="请输入擅长"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
        <el-select v-model="queryParams.proficientIn" placeholder="请选择医生擅长">
          <el-option
            v-for="item in diseases"
            :label="item.dName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="简介" prop="introduce">
        <el-input
          v-model="queryParams.introduce"
          placeholder="请输入简介"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="删除状态" prop="del">
<!--        <el-input-->
<!--          v-model="queryParams.del"-->
<!--          placeholder="请输入删除状态，0未，1删除"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
        <el-select v-model="queryParams.del" placeholder="请选择删除状态">
          <el-option label="未删除" value="0"></el-option>
          <el-option label="已删除" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:docker:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:docker:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:docker:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:docker:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dockerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="医生id" align="center" prop="id" />
      <el-table-column label="医生姓名" align="center" prop="name" />
      <el-table-column label="医生年龄" align="center" prop="age" />
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scop">
          <span v-if="scop.row.sex == '0'">男</span>
          <span v-if="scop.row.sex == '1'">女</span>
        </template>
      </el-table-column>
      <el-table-column label="照片" align="center" prop="img">
        <img style="width: 50px;height: 50px" slot-scope="scope" v-if="scope.row.img!=''" :src="scope.row.img">
      </el-table-column>
      <el-table-column label="职位" align="center" prop="position.pName" />
      <el-table-column label="科室" align="center" prop="department.name" />
      <el-table-column label="擅长" align="center" prop="disNames">
        <template slot-scope="scope">
          <span v-for="item in scope.row.disNames">
            {{item}}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="简介" align="center" prop="introduce" />
      <el-table-column label="删除状态，0未，1删除" align="center" prop="del" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:docker:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:docker:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改医生对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="医生姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入医生姓名" />
        </el-form-item>
        <el-form-item label="医生年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入医生年龄" />
        </el-form-item>
        <el-form-item label="性别">
          <el-radio-group v-model="form.sex">
            <el-radio :label="0" >男</el-radio>
            <el-radio :label="1" >女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="照片" prop="img">
<!--          <el-input v-model="form.img" type="textarea" placeholder="请输入内容" />-->
          <el-upload
            class="avatar-uploader"
            :headers="headers"
            action="http://localhost:8080/system/carousel/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess">
            <img v-if="image" :src="image"class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="职位" prop="positionId">
<!--          <el-input v-model="form.positionId" placeholder="请输入职位id" />-->
          <el-select v-model="form.positionId" placeholder="请选择">
            <el-option
              v-for="item in workers"
              :label="item.pName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="科室" prop="departmentId">
<!--          <el-input v-model="form.departmentId" placeholder="请输入科室id" />-->
<!--          <el-select v-model="form.departmentId" placeholder="请选择">-->
<!--            <el-option-->
<!--              v-for="item in depts"-->
<!--              :label="item.name"-->
<!--              :value="item.id">-->
<!--            </el-option>-->
<!--          </el-select>-->
          <el-cascader
            v-model="treeIds"
            :options="trees"
            :props="areaTree"
            @change="areaCodeClick"></el-cascader>
        </el-form-item>
        <el-form-item label="擅长" prop="proficientIn">
<!--          <el-input v-model="form.proficientIn" placeholder="请输入擅长" />-->
          <el-select v-model="diseaseIds" multiple placeholder="请选择" @change="findDiseaseIds">
            <el-option
              v-for="item in diseases"
              :key="item.id"
              :label="item.dName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="简介" prop="introduce">
          <el-input v-model="form.introduce" placeholder="请输入简介" />
        </el-form-item>
<!--        <el-form-item label="删除状态，0未，1删除" prop="del">-->
<!--          <el-input v-model="form.del" placeholder="请输入删除状态，0未，1删除" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDocker, getDocker, delDocker, addDocker, updateDocker,selectDepts,selectWorkers,selectDeptTrees,selectDiseases } from "@/api/system/docker";
import { getToken } from "@/utils/auth";
export default {
  name: "Docker",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 医生表格数据
      dockerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        age: null,
        sex: null,
        img: null,
        positionId: null,
        departmentId: null,
        proficientIn: null,
        introduce: null,
        del: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      //科室
      depts:[],
      //职位
      workers:[],
      image:null,
      // 设置上传的请求头部
      headers: { Authorization: "Bearer " + getToken() },
      //树数据
      trees:[],
      areaTree:{
        label:"name",
        value:"id",
        children:"department",
        expandTrigger: "hover",
      },
      treeIds:[],
      diseases:[],
      diseaseIds:[],

    };
  },
  created() {
    this.getList();
    this.selectDepts();
    this.selectWorkers();
    this.selectDeptTrees();
    this.findDiseases();
  },
  methods: {
    findDiseaseIds(){
      console.log("疾病ids：",this.diseaseIds)
      this.form.proficientIn = JSON.stringify(this.diseaseIds);
    },

    /** 查询医生列表 */
    getList() {
      this.loading = true;
      listDocker(this.queryParams).then(response => {
        this.dockerList = response.rows;
        console.log("医生数据：",response)
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        age: null,
        sex: null,
        img: null,
        positionId: null,
        departmentId: null,
        proficientIn: null,
        introduce: null,
        createTime: null,
        updateTime: null,
        del: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.treeIds = [];
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.form = {
        id: null,
        name: null,
        age: null,
        sex: null,
        img: null,
        positionId: null,
        departmentId: null,
        proficientIn: null,
        introduce: null,
        createTime: null,
        updateTime: null,
        del: null
      };
      this.diseaseIds = [];
      this.image = null;
      this.trees = [];
      this.treeIds=[];
      this.selectDeptTrees();
      this.selectDepts();
      this.selectWorkers();
      this.findDiseases();
      this.reset();
      this.open = true;
      this.title = "添加医生";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log("修改医生",row)
      this.diseaseIds = [];
      this.selectDepts();
      this.selectWorkers();
      this.findDiseases();
      this.form=row;
      this.treeIds = row.departmentId;
      this.diseaseIds =row.proficientIn.slice(1, -1).split(',').map(num => parseInt(num.trim()));;
      console.log("qwe:",this.diseaseIds)
      this.image = row.img;
      this.reset();
      const id = row.id || this.ids
      getDocker(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改医生";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDocker(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDocker(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除医生编号为"' + ids + '"的数据项？').then(function() {
        return delDocker(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/docker/export', {
        ...this.queryParams
      }, `docker_${new Date().getTime()}.xlsx`)
    },
    //获取科室
    selectDepts(){
      selectDepts().then(res=>{
        // console.log("查看科室：",res)
        this.depts=res.rows;
      })
    },
    //获取科室
    selectWorkers(){
      selectWorkers().then(res=>{
        // console.log("查看职位：",res)
        this.workers=res.rows;
      })
    },
    handleAvatarSuccess(res, file) {
      console.log("返回的图片数据",file)
      this.image = URL.createObjectURL(file.raw); //回显图片
      this.form.img=file.response.data;  //向实体属性传地址
    },
    selectDeptTrees(){
      selectDeptTrees().then(res=>{
        console.log("上下级数据：",res)
        this.trees = res.data;
      })
    },
    areaCodeClick(){
      console.log("科室id集合:",this.treeIds)
      this.form.departmentId = this.treeIds[this.treeIds.length-1];
      console.log("科室id:",this.form.departmentId)
    },
    searchClick(){
      this.queryParams.departmentId = this.treeIds[this.treeIds.length-1];
    },
    findDiseases(){
      selectDiseases().then(res=>{
        console.log("疾病：",res)
        this.diseases = res.rows;
      })
    }

  },
};
</script>

<style>
.avatar-uploader  {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 178px;
  height: 178px;
}
.avatar-uploader:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 200px;
  height: 178px;
  display: block;
}
</style>
